import 'package:flutter/material.dart';
import 'package:flutter_09_game/generated/locales.g.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_09_game/config/app_color.dart';
import 'package:flutter_09_game/generated/assets.dart';
import 'package:flutter_09_game/pages/deposit/models/model_record_team.dart';
import 'package:flutter_09_game/utils/extensions/num_ext.dart';
import 'package:flutter_09_game/pages/deposit/widgets/widget_record_chart.dart';
import 'package:flutter_09_game/widgets/image/auto_image.dart';
import 'package:get/get.dart';

/// 资金列表组件
/// 支持显示"我的收益"和"团队整体汇总结果"两种模式
class WidgetRecordFunds extends StatelessWidget {
  final ModelRecordTeamItem data;

  // final String title;
  final bool? isTeamMode; // true: 团队整体汇总结果, false: 我的收益
  final Widget? userHeader;
  final VoidCallback? onToggleExpand; // 展开/收起回调
  final bool isHash;

  const WidgetRecordFunds({
    super.key,
    required this.data,
    this.isTeamMode,
    this.userHeader,
    this.onToggleExpand,
    this.isHash = false
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(12.w),
      decoration: BoxDecoration(
        color: AppColor.homeTabBackground,
        borderRadius: BorderRadius.circular(12.w),
      ),
      child: Column(
        children: [
          // 标题区域
          _buildHeader(),
          16.verticalSpaceFromWidth,

          // 主要数据区域
          _buildMainData(),
          12.verticalSpaceFromWidth,
          // 详细数据区域
          _buildDetailData(),
        ],
      ),
    );
  }

  /// 构建标题区域
  Widget _buildHeader() {
    if (isTeamMode != null) {
      return SizedBox(
        height: 40.w,
        child: Row(
          children: [
            AutoImage(
              isTeamMode == true
                  ? Assets.depositRecordTeamTag
                  : Assets.depositRecordMineTag,
              width: 24.w,
              height: 24.w,
            ),
            12.horizontalSpace,
            Text(
              isTeamMode == true
                  ? LocaleKeys.teamSummary.tr
                  : LocaleKeys.myEarnings.tr,
              style: TextStyle(
                fontSize: 16.sp,
                fontWeight: FontWeight.w600,
                color: AppColor.textPrimary,
              ),
            ),
            const Spacer(),
            GestureDetector(
              onTap: onToggleExpand,
              child: Container(
                width: 32.w,
                height: 32.w,
                decoration: BoxDecoration(
                    color: AppColor.textPrimary.withOpacity(0.05),
                    borderRadius: BorderRadius.circular(8.w)),
                alignment: Alignment.center,
                child: AnimatedRotation(
                  turns: (data.isExpanded ?? false) ? 0.5 : 0.0, // 0.5 = 180度
                  duration: const Duration(milliseconds: 300),
                  curve: Curves.easeInOut,
                  child: AutoImage(
                    Assets.commonDownArrow,
                    width: 16.w,
                    height: 16.w,
                  ),
                ),
              ),
            )
          ],
        ),
      );
    }
    return Row(
      children: [
        Expanded(child: userHeader!),
        GestureDetector(
          onTap: onToggleExpand,
          child: Container(
            width: 32.w,
            height: 32.w,
            decoration: BoxDecoration(
                color: AppColor.textPrimary.withOpacity(0.05),
                borderRadius: BorderRadius.circular(8.w)),
            alignment: Alignment.center,
            child: AnimatedRotation(
              turns: (data.isExpanded ?? false) ? 0.5 : 0.0, // 0.5 = 180度
              duration: const Duration(milliseconds: 300),
              curve: Curves.easeInOut,
              child: AutoImage(
                Assets.commonDownArrow,
                width: 16.w,
                height: 16.w,
              ),
            ),
          ),
        )
      ],
    );
  }

  /// 构建主要数据区域
  Widget _buildMainData() {
    return Column(
      children: [
        Row(
          children: [
            // 总投注
            Expanded(
              child: _buildMainDataItem(
                label: LocaleKeys.totalBets.tr,
                value: (data.totalBetAmount ?? 0).formatWallet(),
                valueColor: AppColor.textPrimary,
              ),
            ),
            24.horizontalSpace,
            // 总盈利/总亏损
            // total_rebate_amount
            Expanded(
              child: _buildMainDataItem(
                label: getWinAmount() < 0
                    ? LocaleKeys.totalLoss.tr
                    : LocaleKeys.totalProfit.tr,
                // .formatWallet()
                value: _formatMainAmount(),
                valueColor: _getMainAmountColor(),
              ),
            ),
          ],
        ),
        Visibility(
            visible: data.isExpanded == true,
            child: WidgetRecordChart(
              totalBetAmount: (data.totalBetAmount ?? 0) ~/ 1000000,
              amount: getWinAmount() ~/ 1000000,
            ).marginOnly(top: 24.w))
      ],
    );
  }

  /// 构建详细数据区域
  Widget _buildDetailData() {
    double spacing = (1.sw - 56.w - 157.5.w * 2).toInt().toDouble();
    return Wrap(
      spacing: spacing,
      runSpacing: spacing,
      children: [
        _buildDetailDataItem(
          label: LocaleKeys.commission.tr,
          value: (data.totalRebateAmount ?? 0).formatWallet(),
          iconPath: Assets.depositRecordItem1,
        ),
        if (isHash == false)
        _buildDetailDataItem(
          label: LocaleKeys.recharge.tr,
          value: (data.totalRechargeAmount ?? 0).formatWallet(),
          iconPath: Assets.depositRecordItem2,
        ),
        if (isHash == false)
        _buildDetailDataItem(
          label: LocaleKeys.withdraw.tr,
          value: (data.totalWithdrawAmount ?? 0).formatWallet(),
          iconPath: Assets.depositRecordItem3,
        ),
        _buildDetailDataItem(
          label: LocaleKeys.winning.tr,
          value: (data.totalWinCount ?? 0).toString(),
          iconPath: Assets.depositRecordItem4,
        ),
        if (isHash == false)
        _buildDetailDataItem(
          label: LocaleKeys.newUsers.tr,
          value: (data.totalNewUserCount ?? 0).toString(),
          iconPath: Assets.depositRecordItem5,
        ),
        if (isHash == false)
        _buildDetailDataItem(
          label: LocaleKeys.activity.tr,
          value: '0', // 固定值，根据设计图
          iconPath: Assets.depositRecordItem6,
        ),
      ],
    );
  }

  /// 构建主要数据项
  Widget _buildMainDataItem({
    required String label,
    required String value,
    required Color valueColor,
  }) {
    return Column(
      children: [
        Text(
          label,
          style: TextStyle(
            fontSize: 14.sp,
            fontWeight: FontWeight.w500,
            color: AppColor.textPrimary.withOpacity(0.7),
          ),
        ),
        4.verticalSpaceFromWidth,
        Text(
          value,
          style: TextStyle(
            fontSize: 20.sp,
            fontWeight: FontWeight.w900,
            color: valueColor,
          ),
        ),
      ],
    );
  }

  /// 构建详细数据项
  Widget _buildDetailDataItem({
    required String label,
    required String value,
    required String iconPath,
  }) {
    return Container(
      width: 157.5.w,
      height: 63.w,
      padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 12.w),
      decoration: BoxDecoration(
          color: AppColor.navBgColor,
          borderRadius: BorderRadius.circular(8.w),
          image:
              DecorationImage(image: AssetImage(iconPath), fit: BoxFit.cover)),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            label,
            style: TextStyle(
              fontSize: 12.sp,
              fontWeight: FontWeight.w400,
              color: AppColor.textPrimary.withOpacity(0.7),
            ),
          ),
          // 8.verticalSpaceFromWidth,
          Text(
            value,
            style: TextStyle(
              fontSize: 12.sp,
              fontWeight: FontWeight.w900,
              color: AppColor.textPrimary,
            ),
          ),
        ],
      ),
    );
  }

  int getWinAmount() {
    final amount = (data.totalWinAmount ?? 0) -
        (data.totalBetAmount ?? 0) +
        (data.totalRebateAmount ?? 0);
    return amount;
  }

  // /// 格式化主要金额
  String _formatMainAmount() {
    final amount = getWinAmount();

    return "${amount > 0 ? '+' : ''}${amount.formatWallet()}";
  }

  /// 获取主要金额颜色
  Color _getMainAmountColor() {
    final amount = getWinAmount();

    if (amount >= 0) {
      return AppColor.primarySecondary;
    } else {
      return AppColor.gameErrorColor;
    }
  }



}
